/*
 * ASoC Driver for Microsemi DAC
 *
 * Copyright (c) 2018, Microsemi Corporation
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Neither the name of the <organization> nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_platform.h>
#include <linux/clk.h>
#include <sound/soc.h>

#include "imx-audmux.h"
#define MSYS_CLOCK_FOR_ZL380XX_CODEC 24567000

#define DAI_NAME_SIZE	32

struct imx_zl38xx_data {
	struct snd_soc_dai_link dai;
	struct snd_soc_card card;
	char codec_dai_name[DAI_NAME_SIZE];
	char platform_name[DAI_NAME_SIZE];
	unsigned int clk_frequency;
};

static int imx_zl38xx_dai_init(struct snd_soc_pcm_runtime *rtd)
{
	struct imx_zl38xx_data *data = snd_soc_card_get_drvdata(rtd->card);
	struct device *dev = rtd->card->dev;
	int ret;

	ret = snd_soc_dai_set_sysclk(rtd->codec_dai, 0,
				     data->clk_frequency, SND_SOC_CLOCK_IN);
	if (ret) {
		dev_err(dev, "could not set codec driver clock params\n");
		return ret;
	}

	return 0;
}

static int imx_zl38xx_probe(struct platform_device *pdev)
{
	struct device_node *np = pdev->dev.of_node;
	struct device_node *ssi_np, *codec_np;
	struct platform_device *ssi_pdev;
	struct imx_zl38xx_data *data = NULL;
	int int_port, ext_port;
	int ret;

	if (!of_property_read_bool(np, "fsl,no-audmux")) {
		ret = of_property_read_u32(np, "mux-int-port", &int_port);
		if (ret) {
			dev_err(&pdev->dev, "mux-int-port missing or invalid\n");
			return ret;
		}
		ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
		if (ret) {
			dev_err(&pdev->dev, "mux-ext-port missing or invalid\n");
			return ret;
		}

		/*
		 * The port numbering in the hardware manual starts at 1, while
		 * the audmux API expects it starts at 0.
		 */
		int_port--;
		ext_port--;
		ret = imx_audmux_v2_configure_port(int_port,
				IMX_AUDMUX_V2_PTCR_SYN |
				IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
				IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
				IMX_AUDMUX_V2_PTCR_TFSDIR |
				IMX_AUDMUX_V2_PTCR_TCLKDIR,
				IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
		if (ret) {
			dev_err(&pdev->dev, "audmux internal port setup failed\n");
			return ret;
		}
		ret = imx_audmux_v2_configure_port(ext_port,
				IMX_AUDMUX_V2_PTCR_SYN,
				IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
		if (ret) {
			dev_err(&pdev->dev, "audmux external port setup failed\n");
			return ret;
		}
	}

	ssi_np = of_parse_phandle(pdev->dev.of_node, "ssi-controller", 0);
	codec_np = of_parse_phandle(pdev->dev.of_node, "audio-codec", 0);
	if (!ssi_np || !codec_np) {
		dev_err(&pdev->dev, "phandle missing or invalid\n");
		ret = -EINVAL;
		goto fail;
	}

	ssi_pdev = of_find_device_by_node(ssi_np);
	if (!ssi_pdev) {
		dev_err(&pdev->dev, "failed to find SSI platform device\n");
		ret = -EPROBE_DEFER;
		goto fail;
	}

	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
	if (!data) {
		ret = -ENOMEM;
		goto fail;
	}

	data->clk_frequency = MSYS_CLOCK_FOR_ZL380XX_CODEC;

	data->dai.name = "Microsemi DAC";
	data->dai.stream_name = "Microsemi DAC";
	data->dai.codec_dai_name = "zl380xx-dai";
	data->dai.codec_of_node = codec_np;
	data->dai.cpu_of_node = ssi_np;
	data->dai.platform_of_node = ssi_np;
	data->dai.init = &imx_zl38xx_dai_init;
	data->dai.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
			    SND_SOC_DAIFMT_CBM_CFM;

	data->card.dev = &pdev->dev;
	ret = snd_soc_of_parse_card_name(&data->card, "model");
	if (ret)
		goto fail;
	ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing");
	if (ret)
		goto fail;
	data->card.num_links = 1;
	data->card.owner = THIS_MODULE;
	data->card.dai_link = &data->dai;


	platform_set_drvdata(pdev, &data->card);
	snd_soc_card_set_drvdata(&data->card, data);

	ret = devm_snd_soc_register_card(&pdev->dev, &data->card);
	if (ret) {
		dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
		goto fail;
	}

	of_node_put(ssi_np);
	of_node_put(codec_np);

	return 0;

fail:
	of_node_put(ssi_np);
	of_node_put(codec_np);

	return ret;
}

static int imx_zl38xx_remove(struct platform_device *pdev)
{
	struct snd_soc_card *card = platform_get_drvdata(pdev);

	return 0;
}

static const struct of_device_id imx_zl38xx_dt_ids[] = {
	{ .compatible = "microsemi,microsemi-dac", },
	{  }
};
MODULE_DEVICE_TABLE(of, imx_zl38xx_dt_ids);

static struct platform_driver imx_zl38xx_driver = {
	.driver = {
		.name = "imx-zl380xx",
		.pm = &snd_soc_pm_ops,
		.of_match_table = imx_zl38xx_dt_ids,
	},
	.probe = imx_zl38xx_probe,
	.remove = imx_zl38xx_remove,
};
module_platform_driver(imx_zl38xx_driver);

MODULE_AUTHOR("Jean Bony <jean.bony@microsemi.com>");
MODULE_DESCRIPTION("NXP IM7XD zl38xx ASoC machine driver");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:imx-zl38xx");
